package cn.stylefeng.guns.modular.system.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.core.common.annotion.Permission;
import cn.stylefeng.guns.core.common.page.LayuiPageFactory;
import cn.stylefeng.guns.modular.system.entity.Dict;
import cn.stylefeng.guns.modular.system.entity.Student;
import cn.stylefeng.guns.modular.system.model.StudentDto;
import cn.stylefeng.guns.modular.system.service.StudentService;
import cn.stylefeng.guns.modular.system.warpper.DictWrapper;
import cn.stylefeng.guns.modular.system.warpper.StudentWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/stu")
public class StudentController extends BaseController {
    @Autowired
    private StudentService studentService;
    private static String PREFIX = "/modular/system/student/";

    @RequestMapping("")
    public String index() {
        return PREFIX + "student.html";
    }

    @RequestMapping("/student_add_type")
    public String student_add_type(){return PREFIX + "student_add_type.html"; }

    @RequestMapping(value = "/list")
    @ResponseBody
    public Object findStudent(){
        Page<Map<String, Object>> slist = this.studentService.findStudent();
        //StudentWrapper数据传到页面上需要进行一次封装
        Page<Map<String, Object>> warpper = new StudentWrapper(slist).wrap();
        return LayuiPageFactory.createPageInfo(warpper);
    }

    /***
     * 删除执行
     * @param stuId
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public ResponseData delete(@RequestParam Long stuId){
        studentService.delStudent(stuId);
        return SUCCESS_TIP;
    }

    /***
     * 新增保存数据
     * @param stuDto
     * @return
     */
    @RequestMapping(value = "/add")
    @ResponseBody
   public ResponseData add(StudentDto stuDto) {
       studentService.addStudent(stuDto);
       return SUCCESS_TIP;
   }

    /**
     *  内联打开跳转修改
     * @return
     */
   @RequestMapping("/stuPageUpdate")
    public String student_modify_type(@RequestParam("stuid") Integer stuid,Model model) {
            Student student=studentService.selectById(stuid);
       model.addAttribute("student",student);
        return PREFIX+"student_modify_type.html";
    }


    @RequestMapping("/SavestuPageUpdate")
    public @ResponseBody ResponseData SaveStuPageUpdate(Student student,Model model) {
      studentService.updateById(student);
        return SUCCESS_TIP;
    }
}
